#ifndef DECODE_H 
#define DECODE_H

/*************************************************
 * Description 	: -Collect a Byte stream and store it in a reception array
 * 				: -Then decode it to the desired instruction
 *				: -Initialize Timer0 on interupt basis
 * 				: -Then set the interrupt Timer0 and do the sekwence every  mSec
 * Last update 	: See File Date
 * Crystal 		:
 * Email 		:
 * 				: Decode_StateReception    ****************
 *				: 			Decode_WAITSTART (nothing to do waiting on start header )
 *				: 			Decode_STRTDEC (First character(adress recieved)
 *				: 			Decode_FLUSH (Reception is not ours, or character/recieved is garbage  )
 *				: 			Decode_DONE When this state is reached then an action can be executie in the instruction parser(or other logic )
 *				:
 *				: Functions **IN** 	********************
 *				: 	void Decode_AddB (*char) // Add a reception byte to the local instance
 *				:   void Decode_FlushInstr(void) // ReSet the instruction queue
 *				:	void Decode_Clone (*char ,*char) // Clones the current reception buffer to a new one
 *				: 	Decode_InstructionDecoder (unsigned char *Source) // executes the command that is currently in the instruction decoder
 *				: 	void Decode_initTimer0 (void) // interrupt T0 initializer
 *				:
 *				:
 *				: Functions **OUT** 	********************
 *				:
 *				:
 *				:
 *************************************************/

#include <avr/io.h>
#include <stdlib.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>








/* Test interface
	unsigned char Temp ;
	Temp= 0x00;

	Decode_AddB(&Temp);
	Decode_AddB(&Temp);
	Decode_AddB(&Temp);
	Decode_AddB(&Temp);
	Decode_AddB(&Temp);
	Temp = 0x0A; 		// Receiver adrr
	Decode_AddB(&Temp);
	Temp = 0x02; 		// Device adrr
	Decode_AddB(&Temp);
	Temp = 0x03; 		// Command 1
	Decode_AddB(&Temp);
	Temp = 0x04; 		// Command 2
	Decode_AddB(&Temp);
	Temp = 0x05; 		// Val 1
	Decode_AddB(&Temp);
	Temp = 0x06; 		// Val 2
	Decode_AddB(&Temp);
	Temp = 0x07; 		// Val 3
	Decode_AddB(&Temp);
	Temp = 0x08; 		// Val 4
	Decode_AddB(&Temp);
	Temp = 0x08; 		// Bugtest 1 (This char will be ignored)
	Decode_AddB(&Temp);

*/




// *************CONSTANTS
#ifndef  CRYSTAL
	#error 'CRYSTAL VAR UNDEFINED'
#endif

#define Decode_TMR0_STRTCNT (0xFF -(1+((CRYSTAL*10 / 256 )  / 1000))/10) // Even i know this gives a large fault i use it for non critical tasks so ..


#define CNTNULLENGTH 5 // This is the the amount off nul bytes that are required for sync
//#define RECEIVERADDR  10 //This is the device adress (Moved To Main)

 /*************************************************
 * PHRIPHALS	: All DEVICES IMPLEMENTED
 *************************************************/

#define Decode_PHER_SWITCH 0x01 	// First command for a switch action
#define Decode_PHER_DIMPWM 0x02	// Initiate a dim action using hardware pwm register
#define Decode_PHER_DEBUG 0x03	// Generate a 4 Bit Code on output


 /*************************************************
 * COMMANDS		: All Commands implemented
 *************************************************/
// LEVEL 1 Commands


#define Decode_CMD1_SETBYTE 0xFF // This do a logic or on the byte in Value1
#define Decode_CMD1_CLRBYTES 0x11 // This do a logic AND on the byte in Value1
#define Decode_CMD1_SETPWM   0x01 // Set the val1 & val2 in the Hardware PWM regester
#define Decode_CMD1_CLRPWM   0x02 // Clear The PWM To 00
#define Decode_CMD1_DEBUG   0x04  // Generate a 4 Bit Code on output




enum Decode_POS
{
	Decode_RECADR,   		// The ordinal position for the reciever adress byte
	Decode_PHERADR,			// The pheriphal number
	Decode_CMD1,			// Command 1
	Decode_CMD2,			// Command 2
	Decode_VAL1,			// Value 1 That contains the first value needed for the command
	Decode_VAL2,			// Value 2 See Value 1
	Decode_VAL3,			//
	Decode_VAL4				//
};





//__attribute__((packed)) Decode_StateReception = Decode_WAITSTART; // State machine for decoding sequential characters



extern volatile unsigned char  SekTmr_mSecExpired ; 	//Overflow Timer0 interrupt flag
unsigned char Decode_Buff[FRAME_SIZE] ; 		// InstructionBytes Frame Buffer (not yet complete)
unsigned char Decode_InProcess[FRAME_SIZE]; 	// The Current command string in process

extern int16_t Decode_PWM ;


// **************PROTOTYPES *************


extern void Decode_PWMInit (void);
extern void Decode_AddB ( int intTmp);
extern void Decode_FlushInstr ( void );
extern void Decode_Clone  (char *Source , char *Destination  );
extern void Decode_InstructionDecoder (unsigned char *Source);// instruction decoder and Command executer
extern void Decode_initTimer0 (void);

#endif
